package com.zfjt.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00012;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.utils.StringUtil;
import com.zfjt.utils.WQUtil;

/**
 * 传银行流水或房产证明(宣传可以为空)
 * @author xk
 * @version 1.0
 * @date 2017年4月05日
 */
@Controller
public class AddImg04Controller {
	
	private ReturnBean mReturn;
	
	private WQUtil wqUtil;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	private Map<String, String> fhdata; 
	
	@RequestMapping(value="00012")
	@ResponseBody
	public ReturnBean AddImg04(HttpSession hseesion,HttpServletRequest request){
		wqUtil = new WQUtil();
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		System.out.println("111111111111111");
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00012 num00012 = JSON.parseObject(data,PackNum00012.class); 	
		ClientmessageBean mdata = mClientMessage.selectByPrimaryKey((short) Integer.parseInt(num00012.getId()));
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00012");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{	
			//获取Tomcat下项目根目录
			String path = request.getSession().getServletContext().getRealPath("static\\images" + "/") +"\\" + mdata.getName();
			String path1 = request.getSession().getServletContext().getRealPath("static\\images" + "/") +"\\" + "kongtu.jpg";
			//根据用户名新建文件夹存相应照片
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			if (num00012.getImg04_type().equals("1")) {
				mdata.setImg04Type(num00012.getImg04_type());
				if (StringUtil.isNotEmpty(num00012.getImg04_01())) {
					wqUtil.GenerateImage(num00012.getImg04_01(),path + "\\" + "Img04_01" + ".jpg");
					mdata.setImg0401(path + "\\" + "Img04_01" + ".jpg");
				}else {
					mdata.setImg0401(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_02())) {
					wqUtil.GenerateImage(num00012.getImg04_02(),path + "\\" + "Img04_02" + ".jpg");
					mdata.setImg0402(path + "\\" + "Img04_02" + ".jpg");
				}else {
					mdata.setImg0402(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_03())) {
					wqUtil.GenerateImage(num00012.getImg04_03(),path + "\\" + "Img04_03" + ".jpg");
					mdata.setImg0403(path + "\\" + "Img04_03" + ".jpg");
				}else {
					mdata.setImg0403(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_04())) {
					wqUtil.GenerateImage(num00012.getImg04_04(),path + "\\" + "Img04_04" + ".jpg");
					mdata.setImg0404(path + "\\" + "Img04_04" + ".jpg");
				}else {
					mdata.setImg0404(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_05())) {
					wqUtil.GenerateImage(num00012.getImg04_05(),path + "\\" + "Img04_05" + ".jpg");
					mdata.setImg0405(path + "\\" + "Img04_05" + ".jpg");
				}else {
					mdata.setImg0405(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_06())) {
					wqUtil.GenerateImage(num00012.getImg04_06(),path + "\\" + "Img04_06" + ".jpg");
					mdata.setImg0406(path + "\\" + "Img04_06" + ".jpg");
				}else {
					mdata.setImg0406(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_07())) {
					wqUtil.GenerateImage(num00012.getImg04_07(),path + "\\" + "Img04_07" + ".jpg");
					mdata.setImg0407(path + "\\" + "Img04_07" + ".jpg");
				}else {
					mdata.setImg0407(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_08())) {
					wqUtil.GenerateImage(num00012.getImg04_08(),path + "\\" + "Img04_08" + ".jpg");
					mdata.setImg0408(path + "\\" + "Img04_08" + ".jpg");
				}else {
					mdata.setImg0408(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_09())) {
					wqUtil.GenerateImage(num00012.getImg04_09(),path + "\\" + "Img04_09" + ".jpg");
					mdata.setImg0409(path1);
				}else {
					mdata.setImg0409(path + "\\" + "kongtu.jpg");
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_10())) {
					wqUtil.GenerateImage(num00012.getImg04_10(),path + "\\" + "Img04_10" + ".jpg");
					mdata.setImg0410(path1);
				}else {
					mdata.setImg0410(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_11())) {
					wqUtil.GenerateImage(num00012.getImg04_11(),path + "\\" + "Img04_11" + ".jpg");
					mdata.setImg0411(path + "\\" + "Img04_11" + ".jpg");
				}else {
					mdata.setImg0411(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_12())) {
					wqUtil.GenerateImage(num00012.getImg04_12(),path + "\\" + "Img04_12" + ".jpg");
					mdata.setImg0412(path + "\\" + "Img04_12" + ".jpg");
				}else {
					mdata.setImg0412(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_13())) {
					wqUtil.GenerateImage(num00012.getImg04_13(),path + "\\" + "Img04_13" + ".jpg");
					mdata.setImg0413(path + "\\" + "Img04_13" + ".jpg");
				}else {
					mdata.setImg0413(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_14())) {
					wqUtil.GenerateImage(num00012.getImg04_14(),path + "\\" + "Img04_14" + ".jpg");
					mdata.setImg0414(path + "\\" + "Img04_14" + ".jpg");
				}else {
					mdata.setImg0414(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_15())) {
					wqUtil.GenerateImage(num00012.getImg04_15(),path + "\\" + "Img04_15" + ".jpg");
					mdata.setImg0415(path + "\\" + "Img04_15" + ".jpg");
				}else {
					mdata.setImg0417(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_16())) {
					wqUtil.GenerateImage(num00012.getImg04_16(),path + "\\" + "Img04_16" + ".jpg");
					mdata.setImg0416(path + "\\" + "Img04_16" + ".jpg");
				}else {
					mdata.setImg0418(path1);
				}
			}else {
				mdata.setImg04Type(num00012.getImg04_type());
				if (StringUtil.isNotEmpty(num00012.getImg04_17())) {
					wqUtil.GenerateImage(num00012.getImg04_17(),path + "\\" + "Img04_17" + ".jpg");
					mdata.setImg0417(path + "\\" + "Img04_17" + ".jpg");
				}else {
					mdata.setImg0417(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_18())) {
					wqUtil.GenerateImage(num00012.getImg04_18(),path + "\\" + "Img04_18" + ".jpg");
					mdata.setImg0418(path + "\\" + "Img04_18" + ".jpg");
				}else {
					mdata.setImg0418(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_19())) {
					wqUtil.GenerateImage(num00012.getImg04_19(),path + "\\" + "Img04_19" + ".jpg");
					mdata.setImg0419(path + "\\" + "Img04_19" + ".jpg");
				}else {
					mdata.setImg0419(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_20())) {
					wqUtil.GenerateImage(num00012.getImg04_20(),path + "\\" + "Img04_20" + ".jpg");
					mdata.setImg0420(path + "\\" + "Img04_20" + ".jpg");
				}else {
					mdata.setImg0420(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_21())) {
					wqUtil.GenerateImage(num00012.getImg04_21(),path + "\\" + "Img04_21" + ".jpg");
					mdata.setImg0421(path + "\\" + "Img04_21" + ".jpg");
				}else {
					mdata.setImg0421(path1);
				}
				if (StringUtil.isNotEmpty(num00012.getImg04_22())) {
					wqUtil.GenerateImage(num00012.getImg04_22(),path + "\\" + "Img04_22" + ".jpg");
					mdata.setImg0422(path + "\\" + "Img04_22" + ".jpg");
				}else {
					mdata.setImg0422(path1);
				}
			}
			//把完成的接口步骤记录下来
			String str = mdata.getIsaccomplish();
			String reject = mdata.getRejectImg();
			if (num00012.getType().equals("1")) {
				reject=str.replace("img_04,","");
			}
			if (mdata.getType().equals("4")) {
				mdata.setType("2");
			}
			mdata.setIsaccomplish(str + ",img04");
			mClientMessage.updateByPrimaryKeySelective(mdata);
			mReturn.setCode("000");
			fhdata.put("succeed", "1");
			mReturn.setData(JSON.toJSONString(fhdata));
			mReturn.setPortnum("00012");
		}
		return mReturn;
	}
}
